package com.sch.wanandroid.ui.girls.info

import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import coil.load
import com.blankj.utilcode.util.TimeUtils
import com.coder.zzq.smartshow.toast.SmartToast
import com.sch.wanandroid.R
import com.sch.wanandroid.base.BaseActivity
import com.sch.wanandroid.databinding.ActivityGirlsInfoBinding
import com.sch.wanandroid.entity.GirlInfoEntity
import kotlinx.android.synthetic.main.activity_girls.loadingTip
import kotlinx.android.synthetic.main.activity_girls_info.*

/**
 * Created by Sch.
 * Date: 2020/12/16
 * description:
 */
class GirlsInfoActivity : BaseActivity() {
    private lateinit var girlInfoViewModel: GirlInfoViewModel
    private lateinit var databinding: ActivityGirlsInfoBinding
    override fun getLayoutId(): Int {
        databinding = DataBindingUtil.setContentView(this, R.layout.activity_girls_info)
        databinding.lifecycleOwner = this
        return 0
    }

    override fun init(savedInstanceState: Bundle?) {

        girlInfoViewModel = ViewModelProvider(this).get(GirlInfoViewModel::class.java)
        databinding.data = girlInfoViewModel
        val bundle: Bundle? = intent.extras
        val girlsId = bundle?.getString("id")
        girlsId?.let {
            loadingTip.loading()
            girlInfoViewModel.getGirlInfo(it)
//            girlInfoViewModel.getRemark(it)
        }
        girlInfoViewModel.grilInfoLiveData.observe(this) {
            setData(it)
        }
        girlInfoViewModel.errorMsg.observe(this) {
            onError(it)
        }
    }

    private fun onError(it: String?) {
        loadingTip.dismiss()
        SmartToast.error(it)
    }

    private fun setData(it: GirlInfoEntity) {
        loadingTip.dismiss()
        ivGrilInfo.load(it.url)
        databinding.tvCreateDate.text = TimeUtils.getFriendlyTimeSpanByNow(it.createdAt)
    }
}